package com.lanqiao.guimei.dao;

import com.lanqiao.guimei.vo.Admin;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.log4j.Logger;

import java.sql.SQLException;

/**
 * @author 韩原
 * @date 2019/8/13  17:07
 */
public class AdminDao extends BaseDao{
    Logger logger = Logger.getLogger(AdminDao.class);

    /**
     * 登录
     * @param admin
     * @return
     */
    public Admin login(Admin admin) {
        try {
            return runner.query("select user_id userid,login_name loginname,password password " +
                            "from guimei_admin where login_name=? and password=?",
                    new BeanHandler<>(Admin.class),
                    admin.getLoginname(),
                    admin.getPassword());

        } catch (SQLException e) {
            logger.error("登录失败。", e);
        }
        return null;
    }

    /**
     * 查询
     * @param admin
     * @return
     */
    public Admin findly(Admin admin){
        try{
            return runner.query("select * from guimei_admin where user_id=?",
                    new BeanHandler<>(Admin.class),
                    admin.getUserid());
        }catch (SQLException e) {
            logger.error("修改失败。", e);
        }
        return null;
    }
    /**
     * 修改密码
     * @param admin
     * @return
     */
    public Boolean update(Admin admin,Admin admin1,String oldpwd){
        String sql="update guimei_admin set password=? where user_id=? ";
        if (oldpwd.equals(admin1.getPassword())){
            try{
                int update=  runner.update(sql,
                        admin.getPassword(),
                        admin.getUserid());
                return (update==1 ? true : false);
            }catch (SQLException e) {
                logger.error("修改失败。", e);
            }
        }else {
            return false;
        }

        return false;
    }



}
